﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Server.CustomObjects
{
    public class Message
    {
        public enum eCommand
        {
            Debug, Feedback, Text, Location, Picture,
            Reply, Confirmation, Decline, LogIn, LogOut,
            CreateUser, DeleteUser, RenameUser,
            GetContacts, AddContact, RemoveContact, WantToAddYouAsContact, ContactAdded,
            GetSecretKey
        }

        public eCommand Command { get; private set; }
        public String GUID { get; private set; }
        public String RequestGUID { get; private set; }
        public Int32 SourceID { get; private set; }
        public Int32 TargetID { get; private set; }
        public EndPoint TargetEndPoint { get; private set; }
        public String Content { get; private set; }

        public Message(eCommand pCommand, Int32 pSourceID, Int32 pTargetID, String pContent, String pRequestGUID = "", EndPoint pTargetEndpoint = null)
        {
            this.Command = pCommand;
            this.GUID = Guid.NewGuid().ToString();
            this.RequestGUID = pRequestGUID;
            this.SourceID = pSourceID;
            this.TargetID = pTargetID;
            this.TargetEndPoint = pTargetEndpoint;
            this.Content = pContent;
        }

        public Message(String pRawMessage)
        {
            if (pRawMessage != null && pRawMessage.Length > 0 && pRawMessage.Contains("|#|"))
            {
                String[] MessageArray = pRawMessage.Split(new string[] { "|#|" }, StringSplitOptions.None);

                if (MessageArray.Length == 6)
                {
                    // check if COMMAND and GUID is set
                    if (MessageArray[0] != "" || MessageArray[1] != "")
                    {
                        this.Command = (eCommand)Enum.Parse(typeof(eCommand), MessageArray[0]);
                        this.GUID = MessageArray[1];
                        this.RequestGUID = MessageArray[2];
                        this.SourceID = Convert.ToInt32(MessageArray[3]);
                        this.TargetID = Convert.ToInt32(MessageArray[4]);
                        this.Content = MessageArray[5];
                    }
                    else{
                        this.GUID = "-1";
                        //Log.WriteLine("COMMAND or GUID invalid or not set");
                    }
                }
                else
                {
                    this.GUID = "-1";
                    //Log.WriteLine("Message has not enough params");
                }
            }
            else
            {
                this.GUID = "-1";
                //Log.WriteLine("Message invalid");
            }
        }

        public override string ToString()
        {
            String _Message = ((int)Command).ToString();    // add command
            _Message += "|#|" + GUID;                       // add GUID
            _Message += "|#|" + RequestGUID;                // add Requested Message GUID
            _Message += "|#|" + SourceID.ToString();        // add SourceUserId
            _Message += "|#|" + TargetID.ToString();        // add TargetUserId
            _Message += "|#|" + Content;                    // add content
            return _Message;
        }
    }
}
